package com.dongdong.zhuangji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.dongdong.zhuangji.entity.User;
import com.dongdong.zhuangji.mapper.UserMapper;
import com.dongdong.zhuangji.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


/**
 * @author dongdong 抖音号：W13883155245
 * @version 1.0
 * @date 2025/6/9
 * @description 重写org.springframework.security.core.userdetails.UserDetailsService.loadUserByUsername
 */
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    UserMapper userMapper;

    /**
     * 根据用户名查找用户
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user=userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername,username));
        if(user==null){
            return null;
        }

        log.info("loadUserByUsername==============>{}",user.getPassword()) ;
        log.info("loadUserByUsername======123========>{}","$2a$10$Lc/xxVO3PkwSIcE6J.o0MeDI2voTNcugeRA2LKnRltfvn7CuJ3H6K") ;

        /*BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
        String password=encoder.encode(user.getPassword());*/
        String password=user.getPassword();
        String[] authorities={user.getRole()};

        UserDetails userDetails= org.springframework.security.core.userdetails.User.withUsername(user.getUsername()).password(password).authorities(authorities).build();

        log.info("loadUserByUsername==============>{}",userDetails.getPassword()) ;
        return userDetails;

    }
}
